//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.quickserver.util.data;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.quickserver.net.model.GB268753QNS;

public class dataProcessing {
    public dataProcessing() {
    }


    public static String hexTo10StrAll(String hex){
        BigInteger amount = new BigInteger(hex.substring(2), 16);
        return amount.toString();
    }
    public static String strTo10StrAll(String str) {
        BigInteger bInteger = new BigInteger(str, 16);
        return bInteger.toString();
    }

    /**
     * 整数和小数相乘
     * @param a
     * @param b
     * @param c  保留几位小数
     * @return
     */
    public static double multiplication(int a,double b,int c) {
        BigDecimal one = new BigDecimal(a);
        BigDecimal two = new BigDecimal(b);
        double three = one.multiply(two).setScale(c, BigDecimal.ROUND_HALF_UP).doubleValue();
        return three;
    }
    public static String[] bytesToHexStrings(byte[] bArray) {
        new StringBuffer(bArray.length);
        String[] sbs = new String[bArray.length];

        for(int i = 0; i < bArray.length; ++i) {
            String sTemp = Integer.toHexString(255 & bArray[i]);
            if (sTemp.length() < 2) {
                sbs[i] = 0 + sTemp.toUpperCase();
            } else {
                sbs[i] = sTemp.toUpperCase();
            }
        }

        return sbs;
    }

    public static boolean isHaveSerialNumber(List<String> ss, String s) {
        return ss.contains(s);
    }

    public static int hexStringToInt10(String hex) {
        return Integer.parseInt(hex, 16);
    }

    public static String gB18030ByteArrayToUtf8String(byte[] bytes) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        CharBuffer gb18030 = Charset.forName("GB18030").decode(byteBuffer);
        ByteBuffer utf8 = Charset.forName("UTF-8").encode(gb18030);
        return new String(utf8.array());
    }

    public static int sumHexStringToInt10(String a, String b) {
        Integer x = 0;
        x = Integer.parseInt(b + a, 16);
        return x;
    }

    /**
     * 不转换位置
     * @param a
     * @param b
     * @return
     */
    public static int sumHexStringToInt10GD(String a, String b) {
        Integer x = 0;
        x = Integer.parseInt(a + b, 16);
        return x;
    }

    public static String[] strsToStrs(String[] s, int a, int b) {
        String[] ss = new String[b - a + 1];

        for(int i = 0; i < b - a + 1; ++i) {
            ss[i] = s[i + a];
        }

        return ss;
    }

    public static String[] strsToStrs2(String str) {
        String[] strs = new String[]{str.substring(0, 2), str.substring(2)};
        return strs;
    }

    public static String crc_8(String[] strs) {
        byte[] bytes = hexStringsToByteArray(strs);
        int len = SumCheck(bytes, 8).length - 1;
        int sum = SumCheck(bytes, 8)[len];
        int hex_sum_length = Integer.toHexString(sum).length();
        return hex_sum_length == 1 ? "0" + Integer.toHexString(sum).toUpperCase() : Integer.toHexString(sum).substring(hex_sum_length - 2, hex_sum_length).toUpperCase();
    }

    public static String crc_8(String chars) {
        byte[] bytes = hexStrToByteArray(chars);
        int len = SumCheck(bytes, 8).length - 1;
        int sum = SumCheck(bytes, 8)[len];
        int hex_sum_length = Integer.toHexString(sum).length();
        return hex_sum_length == 1 ? "0" + Integer.toHexString(sum).toUpperCase() : Integer.toHexString(sum).substring(hex_sum_length - 2, hex_sum_length).toUpperCase();
    }

    public static byte[] hexStrToByteArray(String str) {
        if (str == null) {
            return null;
        } else if (str.length() == 0) {
            return new byte[0];
        } else {
            byte[] byteArray = new byte[str.length() / 2];

            for(int i = 0; i < byteArray.length; ++i) {
                String subStr = str.substring(2 * i, 2 * i + 2);
                byteArray[i] = (byte)Integer.parseInt(subStr, 16);
            }

            return byteArray;
        }
    }

    public static byte[] hexStringsToByteArray(String[] strs) {
        String s = "";

        int len;
        for(len = 0; len < strs.length; ++len) {
            s = s + strs[len];
        }

        len = s.length();
        byte[] b = new byte[len / 2];

        for(int i = 0; i < len; i += 2) {
            b[i / 2] = (byte)((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
        }

        return b;
    }

    public static byte[] SumCheck(byte[] msg, int length) {
        long mSum = 0L;
        byte[] mByte = new byte[length];
        byte[] var8 = msg;
        int var7 = msg.length;

        for(int var6 = 0; var6 < var7; ++var6) {
            byte byteMsg = var8[var6];
            long mNum = (long)byteMsg >= 0L ? (long)byteMsg : (long)byteMsg + 256L;
            mSum += mNum;
        }

        for(int liv_Count = 0; liv_Count < length; ++liv_Count) {
            mByte[length - liv_Count - 1] = (byte)((int)(mSum >> liv_Count * 8 & 255L));
        }

        return mByte;
    }

    public static String edition(String[] strs) {
        int one = Integer.parseInt(strs[0], 16);
        int two = Integer.parseInt(strs[1], 16);
        return one + "." + two;
    }

    public static String strsToTime(String[] strs) {
        if (strs.length == 6) {
            String miao = hexStringToInt10(strs[0]) < 10 ? "0" + hexStringToInt10(strs[0]) : String.valueOf(hexStringToInt10(strs[0]));
            String fen = hexStringToInt10(strs[1]) < 10 ? "0" + hexStringToInt10(strs[1]) : String.valueOf(hexStringToInt10(strs[1]));
            String shi = hexStringToInt10(strs[2]) < 10 ? "0" + hexStringToInt10(strs[2]) : String.valueOf(hexStringToInt10(strs[2]));
            String ri = hexStringToInt10(strs[3]) < 10 ? "0" + hexStringToInt10(strs[3]) : String.valueOf(hexStringToInt10(strs[3]));
            String yue = hexStringToInt10(strs[4]) < 10 ? "0" + hexStringToInt10(strs[4]) : String.valueOf(hexStringToInt10(strs[4]));
            String nian = hexStringToInt10(strs[5]) < 10 ? "0" + hexStringToInt10(strs[5]) : String.valueOf(hexStringToInt10(strs[5]));
            return "20" + nian + "-" + yue + "-" + ri + " " + shi + ":" + fen + ":" + miao;
        } else {
            return "";
        }
    }

    public static String strToLen9(String strs, int num) {
        int strlen = strs.length();
        if (strs.length() >= num) {
            return strs;
        } else {
            for(int i = 0; i < num - strlen; ++i) {
                strs = "0" + strs;
            }

            return strs;
        }
    }

    public static String[] reInfo(String str) {
        String[] strs = new String[4];
        int s = Integer.parseInt(str);
        String s16 = addZeroForNum(decimalToHex(s), 8);
        strs[0] = s16.substring(0, 2);
        strs[1] = s16.substring(2, 4);
        strs[2] = s16.substring(4, 6);
        strs[3] = s16.substring(6, 8);
        return strs;
    }

    public static String[] reInfo(String lx, String[] info) {
        String[] reIn = new String[3];
        String whL = "";
        String whH = "";
        String qhL = "";
        String qhH = "";
        String zong = "";
        int infoLength = info.length;
        if (infoLength == 4) {
            whL = info[0];
            whH = info[1];
            qhL = info[2];
            qhH = info[3];
            if ("0101".equals(lx)) {
                zong = strToLen9(String.valueOf(hexStringToInt10(qhH + qhL + whH + whL)), 9);
                reIn[0] = String.valueOf(Integer.parseInt(zong.substring(0, 3)));
                reIn[1] = String.valueOf(Integer.parseInt(zong.substring(3, 6)));
                reIn[2] = String.valueOf(Integer.parseInt(zong.substring(6, 9)));
                return reIn;
            } else if ("0102".equals(lx)) {
                reIn[0] = strToLen9(String.valueOf(hexStringToInt10(qhH + qhL + whH + whL)), 6);
                return reIn;
            } else if ("0103".equals(lx)) {
                zong = strToLen9(String.valueOf(hexStringToInt10(qhH + qhL + whH + whL)), 9);
                reIn[0] = String.valueOf(Integer.parseInt(zong.substring(0, 3)));
                reIn[1] = String.valueOf(Integer.parseInt(zong.substring(3, 6)));
                reIn[2] = String.valueOf(Integer.parseInt(zong.substring(6, 9)));
                return reIn;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public static String getUniqueCode(String[] strs) {
        String uniqueCode = "";
        int strLen = strs.length - 1;

        for(int i = strLen; i > -1; --i) {
            uniqueCode = uniqueCode + strs[i];
        }

        return uniqueCode;
    }

    public static int[] henTo2(String s) {
        int[] z = new int[8];
        int num = Integer.parseInt(s, 16);
        String str = "";

        String strs;
        for(strs = ""; num != 0; num /= 2) {
            str = num % 2 + str;
        }

        for(int i = 0; i < 8 - str.length(); ++i) {
            strs = strs + "0";
        }

        strs = strs + str;
        StringBuilder bd_str = new StringBuilder(strs);
        strs = bd_str.toString();

        for(int i = 0; i < strs.length(); ++i) {
            z[i] = Integer.parseInt(strs.substring(i, i + 1));
        }

        return z;
    }

    public static String strsToStr(String[] strs) {
        String str = "";

        for(int i = 0; i < strs.length; ++i) {
            str = str + strs[i];
        }

        return str;
    }

    public static byte[] strToBytes(String str) {
        if (str.length() % 2 == 1) {
            return null;
        } else {
            String[] strs = new String[str.length() / 2];
            int j = 0;

            for(int i = 0; i < strs.length; ++i) {
                strs[i] = str.substring(j, j + 2);
                j += 2;
            }

            return hexStringsToByteArray(strs);
        }
    }

    public static String[] strToStrs(String str) {
        if (str.length() % 2 == 1) {
            return null;
        } else {
            String[] strs = new String[str.length() / 2];
            int j = 0;

            for(int i = 0; i < strs.length; ++i) {
                strs[i] = str.substring(j, j + 2);
                j += 2;
            }

            return strs;
        }
    }

    public static String decimalToHex(int decimal) {
        String hex;
        for(hex = ""; decimal != 0; decimal /= 16) {
            int hexValue = decimal % 16;
            hex = toHexChar(hexValue) + hex;
        }

        return hex;
    }

    public static char toHexChar(int hexValue) {
        return hexValue <= 9 && hexValue >= 0 ? (char)(hexValue + 48) : (char)(hexValue - 10 + 65);
    }

    public static String addZeroForNum(String str, int strLength) {
        int strLen = str.length();
        if (strLen < strLength) {
            while(strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                sb.append("0").append(str);
                str = sb.toString();
                strLen = str.length();
            }
        }

        return str;
    }

    public static String[] getDateTime16(Date date) {
        int year = Calendar.getInstance().get(1);
        int month = Calendar.getInstance().get(2)+1;
        int day = Calendar.getInstance().get(5);
        int hours = Calendar.getInstance().get(11);
        int minutes = Calendar.getInstance().get(12);
        int seconds = Calendar.getInstance().get(13);
        String yearStr = String.valueOf(year);
        String year16 = decimalToHex(Integer.parseInt(yearStr.substring(yearStr.length() - 2)));
        String month16 = decimalToHex(month);
        String day16 = decimalToHex(day);
        String hours16 = decimalToHex(hours);
        String minutes16 = decimalToHex(minutes);
        String seconds16 = decimalToHex(seconds);
        String[] dates = new String[]{addZeroForNum(seconds16, 2), addZeroForNum(minutes16, 2), addZeroForNum(hours16, 2), addZeroForNum(day16, 2), addZeroForNum(month16, 2), addZeroForNum(year16, 2)};
        return dates;
    }
    public static String fromHexString(String hexString) throws Exception {
        // 用于接收转换结果
        String result = "";
        // 转大写
        hexString = hexString.toUpperCase();
        // 16进制字符
        String hexDigital = "0123456789ABCDEF";
        // 将16进制字符串转换成char数组
        char[] hexs = hexString.toCharArray();
        // 能被16整除，肯定可以被2整除
        byte[] bytes = new byte[hexString.length() / 2];
        int n;

        for (int i = 0; i < bytes.length; i++) {
            n = hexDigital.indexOf(hexs[2 * i]) * 16 + hexDigital.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        // byte[]--&gt;String
        result = new String(bytes, "UTF-8");
        return result;
    }
    /**
     * 二进制转十进制
     * @param number
     * @return
     */
    public static int binary2Decimal(String number) {
        return scale2Decimal(number, 2);
    }
    /**
     * 其他进制转十进制
     * @param number
     * @return
     */
    public static int scale2Decimal(String number, int scale) {
        checkNumber(number);
        if (2 > scale || scale > 32) {
            throw new IllegalArgumentException("scale is not in range");
        }
        // 不同其他进制转十进制,修改这里即可
        int total = 0;
        String[] ch = number.split("");
        int chLength = ch.length;
        for (int i = 0; i < chLength; i++) {
            total += Integer.valueOf(ch[i]) * Math.pow(scale, chLength - 1 - i);
        }
        return total;

    }
    public static void checkNumber(String number) {
        String regexp = "^\\d+$";
        if (null == number || !number.matches(regexp)) {
            throw new IllegalArgumentException("input is not a number");
        }
    }
    /**
     * 十进制转其他进制
     * @param number
     * @param scale
     * @return
     */
    public static String decimal2Scale(int number, int scale) {
        if (2 > scale || scale > 32) {
            throw new IllegalArgumentException("scale is not in range");
        }
        String result = "";
        while (0 != number) {
            result = number % scale + result;
            number = number / scale;
        }

        return result;
    }
    /**
     * @description 将二进制转换成16进制
     *
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    public static byte bit2byte(String bString){
        byte result=0;
        for(int i=bString.length()-1,j=0;i>=0;i--,j++){
            result+=(Byte.parseByte(bString.charAt(i)+"")*Math.pow(2, j));
        }
        return result;
    }
    /**

     * 原码转反码

     *

     * @param number

     * @return

     */

    public static String binOriginalToBack(String number) {

   /* if (number.startsWith("0")) {

    return number;

        }else {*/

        StringBuffer sbf =new StringBuffer();

        sbf.append("1");

        String f_str = number.substring(1);

        for (int i =0; i < f_str.length(); i++) {

            String s_str = String.valueOf(f_str.charAt(i));

            if (s_str.equals("0")) {

                sbf.append("1");

            }else if (s_str.equals("1")) {

                sbf.append("0");

            }

        }

        return sbf.toString();

        // }

    }

    /**

     * 反码转补码

     *

     * @param

     * @return

     */

    public static String binBackToRepair(String a, String b) {

        if (a.startsWith("0")) {

            return a;

        }

        StringBuilder sb =new StringBuilder();

        int x =0;

        int y =0;

        int pre =0;//进位

        int sum =0;//存储进位和另两个位的和

        while (a.length() != b.length()) {//将两个二进制的数位数补齐,在短的前面添0

            if (a.length() > b.length()) {

                b ="0" + b;

            }else {

                a ="0" + a;

            }

        }

        for (int i = a.length() -1; i >=0; i--) {

            x = a.charAt(i) -'0';

            y = b.charAt(i) -'0';

            sum = x + y + pre;//从低位做加法

            if (sum >=2) {

                pre =1;//进位

                sb.append(sum -2);

            }else {

                pre =0;

                sb.append(sum);

            }

        }

        if (pre ==1) {

            sb.append("1");

        }

        return sb.reverse().toString();//翻转返回

    }
    /**
     * 将二进制字符串转换成int数组
     * @param binStr
     * @return
     */
    private int[] BinstrToIntArray(String binStr) {
        char[] temp = binStr.toCharArray();
        int[] result = new int[temp.length];
        for (int i = 0; i < temp.length; i++) {
            result[i] = temp[i] - 48;
        }
        return result;
    }
    public static String hexString2binaryString(String hexString) {
        if (hexString == null || hexString.length() % 2 != 0)
            return null;
        String bString = "", tmp;
        for (int i = 0; i < hexString.length(); i++) {
            tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
            bString += tmp.substring(tmp.length() - 4);
        }
        return bString;
    }
    //------------------------------------------------------
    public static String binaryString2hexString(String bString) {
        if (bString == null || bString.equals("") || bString.length() % 8 != 0)
            return null;
        StringBuffer tmp=new StringBuffer();
        int iTmp = 0;
        for (int i = 0; i < bString.length(); i += 4) {
            iTmp = 0;
            for (int j = 0; j < 4; j++) {
                iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
            }
            tmp.append(Integer.toHexString(iTmp));
        }
        return tmp.toString();
    }

    /**
     * 以大端模式将byte[]转成int
     */
    public static int bytesToIntBig(byte[] src, int offset) {
        int value;
        String[] strs = dataProcessing.bytesToHexStrings(src);
        int[] strs2 = dataProcessing.henTo2(strs[0]);
        if (strs2[0] == 1) {
            int a1=-dataProcessing.hexStringToInt10(strs[1]+strs[2]+strs[3]);
            return a1;
            /*String a1 = binOriginalToBack(hexString2binaryString(strs[0]));
            String a2 = binOriginalToBack(hexString2binaryString(strs[1]));
            String a3 = binOriginalToBack(hexString2binaryString(strs[2]));
            String a4 = binOriginalToBack(hexString2binaryString(strs[3]));

            String z=a1+a2+a3+a4;
            Integer.toBinaryString(Integer.parseInt(z))+1;

            int a4s = dataProcessing.binary2Decimal(a4);
            a4 = dataProcessing.decimal2Scale(a4s, 2);



            a1 = dataProcessing.decimalToHex(dataProcessing.binary2Decimal(a1));
            a2 = dataProcessing.decimalToHex(dataProcessing.binary2Decimal(a2));
            a3 = dataProcessing.decimalToHex(dataProcessing.binary2Decimal(a3));
            a4 = dataProcessing.decimalToHex(dataProcessing.binary2Decimal(a4));

            String[] abxdys = {a1, a2, a3, a4};
            byte[] abxdyByte = dataProcessing.hexStringsToByteArray(abxdys);
            src = abxdyByte;

            return ((src[offset] & 0xFF) << 24)
                    | ((src[offset + 1] & 0xFF) << 16)
                    | ((src[offset + 2] & 0xFF) << 8)
                    | (src[offset + 3] & 0xFF);*/
        } else {
            value = (int) (((src[offset] & 0xFF) << 24)
                    | ((src[offset + 1] & 0xFF) << 16)
                    | ((src[offset + 2] & 0xFF) << 8)
                    | (src[offset + 3] & 0xFF));
            return value;
        }
    }


    /**
     * 以大端模式将int转成byte[]
     */
    public static byte[] intToBytesBig(int value) {
        byte[] src = new byte[4];
        src[0] = (byte) ((value >> 24) & 0xFF);
        src[1] = (byte) ((value >> 16) & 0xFF);
        src[2] = (byte) ((value >> 8) & 0xFF);
        src[3] = (byte) (value & 0xFF);
        return src;
    }

    /**
     * 以小端模式将int转成byte[]
     *
     * @param value
     * @return
     */
    public static byte[] intToBytesLittle(int value) {
        byte[] src = new byte[4];
        src[3] = (byte) ((value >> 24) & 0xFF);
        src[2] = (byte) ((value >> 16) & 0xFF);
        src[1] = (byte) ((value >> 8) & 0xFF);
        src[0] = (byte) (value & 0xFF);
        return src;
    }

    /**
     * 以小端模式将byte[]转成int
     */
    public static int bytesToIntLittle(byte[] src, int offset) {
        int value;
        value = (int) ((src[offset] & 0xFF)
                | ((src[offset + 1] & 0xFF) << 8)
                | ((src[offset + 2] & 0xFF) << 16)
                | ((src[offset + 3] & 0xFF) << 24));
        return value;
    }

    /**
     * 多数组合并
     * @param arrays
     * @return
     */
    public static int[] arrayCopy(int[]... arrays){
        int arrayLength = 0;
        int startIndex = 0;
        for(int[] file : arrays){
            arrayLength = arrayLength + file.length;
        }
        int[] fileArray = new int[arrayLength];
        for(int i = 0; i < arrays.length; i++){
            if(i > 0){
                startIndex = startIndex + arrays[i-1].length;
            }
            System.arraycopy(arrays[i], 0, fileArray, startIndex, arrays[i].length);
        }
        return fileArray;
    }

    public static String stringToAscii(String value)
    {
        StringBuffer sbu = new StringBuffer();
        char[] chars = value.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(i != chars.length - 1)
            {
                sbu.append((int)chars[i]).append(",");
            }
            else {
                sbu.append((int)chars[i]);
            }
        }
        return sbu.toString();
    }

    public static Float hexToFloat(String hex) {
        Float value = Float.intBitsToFloat(Integer.valueOf(hex.trim(), 16));
        return value;
    }

    public static double getUdisp(int u,float pt){
        double v = u * 0.1 * pt;
        return v;
    }

    public static double getIdisp(int u,float cp){
        double v = u * 0.001 * cp;
        return v;
    }
    public static void main(String[] args) throws Exception {
    	//String [] a1=dataProcessing.strsToStrs2("404001000102180A0F0F0614000000000000468C68F80D002B00027A01C80000E90300000200010400000000CDCCCC3D0FB5DA31CDA8B5C0B5CDCFDEB1A8BEAF1E0A0F0F0614B02323");
        String uStr[] = dataProcessing.strsToStrs2("8889000000000001");
        byte aa[]=dataProcessing.hexStringsToByteArray(uStr);
        String uniqueCode = dataProcessing.gB18030ByteArrayToUtf8String(aa);

        String uStr1[] = dataProcessing.strToStrs("8811100000000108");
        String uniqueCode1 = dataProcessing.strsToStr(uStr1);
        System.out.println(uniqueCode1);


      /*  3F800000
        41F00000*/
    }
}
